/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package plar.core.defaultActions;

import java.awt.Point;

import plar.core.Action;
import plar.core.Common;
import plar.core.Element;
import plar.core.Parameters;

public class Move implements Action {
	Point exspeed;
	Boolean persist;
	public Point speed;
	Integer times;
	Integer totalTimes;

	public Move() {

		speed = new Point();
		persist = true;
	}

	@Override
	public Parameters call(Parameters parameters) {
		String subaction = (String) parameters.get("subAction");
		Element me = (Element) parameters.get("me");

		boolean collision = (Boolean) me.getProperty("isColliding");

		// Point s = me.getSpeed();
		if (subaction == "new") {

			speed = (Point) parameters.get("speed");
			totalTimes = (Integer) parameters.get("totalTimes");
			persist = (Boolean) parameters.get("persist");
			times = 0;
			// Common.info("new moviment "+speed+" totalTimes"+totalTimes);
		}
		if (collision) {
			boolean xcollision = (Boolean) me.getProperty("isXColliding");
			boolean ycollision = (Boolean) me.getProperty("isYColliding");
			if ((speed.x != 0 && xcollision) || (speed.y != 0 && ycollision)) {
				persist = true;
				subaction = "stop";
				Common.info("MOve: collision!");
			}
		}
		if ((subaction == "stop")) {
			times = 0;
			totalTimes = 0;

			// Common.info(""+me+" -> stop");
			// Common.info("stop moving and persist="+persist);
			if (!persist) {
				// Point s = (Point)me.getProperty(".speed");
				Point s = me.getSpeed();
				s.x -= speed.x;
				s.y -= speed.y;

				// Point newDotSpeed= new Point(0,0);
				// me.setProperty(".speed", newDotSpeed);
				me.setSpeed(s);
			}
			persist = true;
			speed = new Point(0, 0);

		}
		if (subaction == "main" && totalTimes != null && totalTimes > 0) {

			if (times < totalTimes) {
				Point s = (Point) me.getProperty(".speed");
				// Point newDotSpeed= new
				// Point(s.x+(speed.x*(times+1)),s.y+(speed.y*(times+1)));
				Point newDotSpeed = new Point(s.x + (speed.x), s.y + (speed.y));
				me.setProperty(".speed", newDotSpeed);
				// if(speed.y<0) Common.info("jump! with"+newDotSpeed);
				// me.setSpeed(new Point(s.x+speed.x,s.y+speed.y));
				times++;
				// Common.info(""+me+" -> main"+" speed:"+me.getSpeed());
			} else {
				if (!persist) {
					// me.setSpeed(new
					// Point(s.x-(totalTimes*speed.x),s.y-(totalTimes*speed.y)));
				}
				totalTimes = 0;
				times = 0;
			}

		}
		return null;
	}
}